package com.zx.platform.utils;

import com.zx.map.utils.MyBeanUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;


import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 15-5-31
 * Time: 上午11:10
 * To change this template use File | Settings | File Templates.
 */
public class HttpClientCommon {
	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(HttpClientCommon.class);

	/***
	 * 获取接口返回
	 *
	 * @param <T>
	 * @param clazz
	 * @param getUrl
	 * @return
	 * @author Chen
	 */
	public <T> T getResult(Class<T> clazz, String getUrl) {
		String result = getResultByGet(getUrl);
		return MyBeanUtils.jsonToBean(result, clazz);
	}

	/**
	 * 获取返回信息
	 *
	 * @param getUrl get
	 * @return
	 */
	public String getResultByGet(String getUrl) {
		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setContentCharset("UTF-8");
		String returnStr = "";
		//方法拿出来
		GetMethod method = new GetMethod(getUrl);
		method.getParams().setContentCharset("UTF-8");
		try {
			//链接超时 2分钟超时
			httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(120000);
			// 读取超时   2分钟超时
			httpClient.getHttpConnectionManager().getParams().setSoTimeout(120000);
			httpClient.executeMethod(method);
			returnStr = method.getResponseBodyAsString();
			log.info("end transfer  returnStr" + returnStr);
		} catch (HttpException e) {
			log.error("HttpException exception", e);
			throw new RuntimeException("9999");//上层系统必须自己处理
		} catch (IOException e) {
			log.error("IOException exception", e);
			throw new RuntimeException("9999");//上层系统必须自己处理
		} catch (Exception ex) {
			log.error("Exception exception", ex);
			throw new RuntimeException("9999");//上层系统必须自己处理
		}
		return returnStr;
	}

	/***
	 * 获取接口返回 post
	 *
	 * @param <T>
	 * @param clazz
	 * @param getUrl
	 * @return
	 * @author jeffery
	 */
	public <T> T getResult(Class<T> clazz, String getUrl, Map<String, String> params) {
		String result = getResultByPost(getUrl, params);
		log.info("request response " + result);
		return MyBeanUtils.jsonToBean(result, clazz);
	}

	/**
	 * 获取返回信息
	 *
	 * @param params
	 * @return
	 */
	public String getResultByPost(String postUrl, Map<String, String> params) {
		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setContentCharset("UTF-8");
		String returnStr = "";
		//方法拿出来
		PostMethod method = new PostMethod(postUrl);
		Set<String> keySet = params.keySet();
		for (String key : keySet) {
			if (null != params.get(key)) {
				method.addParameter(key, params.get(key));
			}
		}
		HttpMethodParams param = method.getParams();
		param.setContentCharset("UTF-8");
		try {
			//链接超时 2分钟超时
			httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(120000);
			// 读取超时   2分钟超时
			httpClient.getHttpConnectionManager().getParams().setSoTimeout(120000);
			httpClient.executeMethod(method);
			InputStream in = method.getResponseBodyAsStream();
			returnStr = PmpUtils.inputStream2String(in, "UTF-8");  //UTF-8
		} catch (HttpException e) {
			log.error("HttpException exception", e);
			throw new RuntimeException("9999");//上层系统必须自己处理
		} catch (IOException e) {
			log.error("IOException exception", e);
			throw new RuntimeException("9999");//上层系统必须自己处理
		} catch (Exception ex) {
			log.error("Exception exception", ex);
			throw new RuntimeException("9999");//上层系统必须自己处理
		}
		return returnStr;
	}

	/***
	 * 获取接口返回 post
	 *
	 * @param <T>
	 * @param clazz
	 * @param getUrl
	 * @return
	 * @author jeffery
	 */
	public <T> T getResult(Class<T> clazz, String getUrl, String jsonParams) {
		String result = getResultByPost(getUrl, jsonParams);
		log.info("request response " + result);
		return MyBeanUtils.jsonToBean(result, clazz);
	}


	/**
	 * 获取返回信息
	 *
	 * @param jsonParams
	 * @return
	 */
	public String getResultByPost(String postUrl, String jsonParams) {
		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setContentCharset("UTF-8");
		String returnStr = "";
		try {
			//方法拿出来
			PostMethod method = new PostMethod(postUrl);
			RequestEntity entity = new StringRequestEntity(jsonParams, "text/html;charset=UTF-8", "UTF-8");
			method.setRequestEntity(entity);
			HttpMethodParams param = method.getParams();
			param.setContentCharset("UTF-8");
			httpClient.executeMethod(method);
			InputStream in = method.getResponseBodyAsStream();
			returnStr = PmpUtils.inputStream2String(in, "UTF-8");  //UTF-8
		} catch (HttpException e) {
			log.error("HttpException exception", e);
			throw new RuntimeException("9999");//上层系统必须自己处理
		} catch (IOException e) {
			log.error("IOException exception", e);
			throw new RuntimeException("9999");//上层系统必须自己处理
		} catch (Exception ex) {
			log.error("Exception exception", ex);
			throw new RuntimeException("9999");//上层系统必须自己处理
		}
		return returnStr;
	}

	/***
	 * add by wenjw 增加post请求
	 * 获取接口返回
	 *
	 * @param <T>
	 * @param clazz
	 * @param getUrl
	 * @return
	 * @author Chen
	 */
	public <T> T getResult(Class<T> clazz, String getUrl, String requestType, String postJson) {
		String result = getClientResultStr(getUrl, requestType, postJson);
		return MyBeanUtils.jsonToBean(result, clazz);
	}

	public String getClientResultStr(String getUrl, String requestType, String postJson) {
		HttpClient httpClient = new HttpClient();
		String returnStr = "";
		//方法拿出来
		if ("post".equals(requestType)) {
			PostMethod method = new PostMethod(getUrl);
			method.addParameter("params", postJson);
			HttpMethodParams param = method.getParams();
			param.setContentCharset("UTF-8");
			try {
				httpClient.executeMethod(method);
				InputStream in = method.getResponseBodyAsStream();
				returnStr = PmpUtils.inputStream2String(in, "UTF-8");  //UTF-8
			} catch (HttpException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			GetMethod method = new GetMethod(getUrl);
			method.getParams().setContentCharset("UTF-8");
			try {
				httpClient.executeMethod(method);
				InputStream in = method.getResponseBodyAsStream();
				returnStr = PmpUtils.inputStream2String(in, "UTF-8");  //UTF-8
			} catch (HttpException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return returnStr;
	}

}
